package language

import (
	"strings"

	"gddgame.cc/galaxy/common/dsl/db"
	"gddgame.cc/galaxy/common/dsl/game"
	"gddgame.cc/galaxy/utils"
)

const providerName = "language"

func init() {
	_ = game.RegisterGlobalModule(game.ModuleProvider{
		Name:    providerName,
		Depends: []string{"group"},
		OnInit: func(env *game.Environment) error {
			var err error
			if _, err = env.RegisterTable(SettingLanguageName, &SettingLanguage{}, &db.Table{
				Format: func(data map[string]interface{}) {
					if v, ok := data["key"]; ok {
						if v == nil || v == "" {
							v = data["id"]
						}
						data["key"] = "language:" + utils.ToString(v)
					}
				},
				CanEdit: func(id string, data map[string]interface{}) error {
					if v, ok := data["key"]; ok {
						data["key"] = strings.Replace(utils.ToString(v), "language:", "", 1)
					}
					return nil
				},
			}); err != nil {
				return err
			}
			return nil
		},
	})
}
